﻿/********************************************************
 * Module/Class Name    : Database Metadata
 * Purpose              : Fetches the database table and columns
 * Chronological Development
 * Jagmohan Bhatt   3-Sep-2009
 ******************************************************/
using System;
using System.Data;
using System.Data.OleDb;
using VAdvantage.DataBase;
using System.Collections.Generic;
using System.Collections;
using System.Data.Common;
using Npgsql;

namespace VAdvantage.Utility
{
/// <summary>
/// Summary description for DatabaseMetaData.
/// </summary>
    public class DatabaseMetaData
    {
        /// <summary>
        /// The Connection Object
        /// </summary>
        OleDbConnection connection_1;
        DbConnection connection_;
        /// <summary>
        /// Default No-Argument constructor that
        /// Creates a connection to the database
        /// </summary>
        public DatabaseMetaData()
        {

            DbProviderFactory dbProviderFactory = null;
            string CONNECTION_STRING = Ini.CreateConnectionString();

            if (DatabaseType.IsOracle)
            {
                dbProviderFactory = DbProviderFactories.GetFactory("System.Data.OracleClient");
            }
            else if (DatabaseType.IsPostgre)
            {
                dbProviderFactory = DbProviderFactories.GetFactory("Npgsql");
            }
            try
            {
                connection_ = dbProviderFactory.CreateConnection();
                connection_.ConnectionString = Ini.CreateConnectionString();
                connection_.Open();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Closes the Connection to the Database
        /// </summary>
        void Dispose()
        {
            this.connection_.Close();
        }

        /// <summary>
        /// Retrieves Database Metadata information about Tables
        /// of the specific database exposed to this user
        /// </summary>
        public DataSet GetTables()
        {
            DataSet ds = new DataSet();
            //DataTable tables = this.connection_1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            //DataTable tables = this.connection_.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            DataTable tables1 = this.connection_.GetSchema("Tables", null);
            ds.Tables.Add(tables1);
            return ds;
        }


                /// <summary>
        /// Retrieves Database Metadata information about Columns
        /// of the specific database exposed to this user
        /// </summary>
        public DataSet GetIndexInfo(string catalog, string schema, string indexName)
        {
            DataSet ds = null;
            try
            {

                ds = new DataSet();
                //DataTable tables1 = this.connection_1.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, schema, "VIENNA_2", null });
                DataTable tables = null;
                if (DatabaseType.IsPostgre)
                    tables = this.connection_.GetSchema("IndexColumns", new string[] { catalog, VConnection.Get().Db_name, indexName.ToLower(), null });
                else
                    tables = this.connection_.GetSchema("IndexColumns", new string[] { null, indexName, schema });

                ds.Tables.Add(tables);
            }
            catch
            {

            }
            return ds;
        }

        /// <summary>
        /// Retrieves Database Metadata information about Columns
        /// of the specific database exposed to this user
        /// </summary>
        public DataSet GetColumns(string catalog, string schema, string tableName)
        {
            DataSet ds = null;
            try
            {

                ds = new DataSet();
                //DataTable tables1 = this.connection_1.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, schema, "VIENNA_2", null });
                DataTable tables = null;
                if (DatabaseType.IsPostgre)
                    tables = this.connection_.GetSchema("Columns", new string[] { catalog, VConnection.Get().Db_name, tableName.ToLower(), null });
                else
                    tables = this.connection_.GetSchema("Columns", new string[] { schema, tableName });

                ds.Tables.Add(tables);
            }
            catch
            {

            }
            return ds;
        }
    }

    public class Types
    {

        public const string DATE = "DATE";
        public const string TIME = "TIME";
        public const string TIMESTAMP = "TIMESTAMP";
        public const string NUMBER = "NUMBER";
        public const string INTEGER = "INTEGER";
        public const string NUMERIC = "NUMERIC";

        public const string BLOB = "BLOB";
        public const string CLOB = "CLOB";

        public const string CHAR = "CHAR";
        public const string VARCHAR = "NVARCHAR"; 
        public const string DECIMAL = "NUMBER";
        public const string SMALLINT = "NUMBER";

    }
}